
#define BLINKER_PRINT Serial
#define BLINKER_WIFI
// #define BLINKER_BLE
#include <Blinker.h>

#include <IRremoteESP8266.h>
#include <IRsend.h>
// #include <ir_Kelvinator.h>
// #include <ir_Gree.h>
#include <ir_Electra.h>


char auth[] = "3a60a8712e74";//这里面填写设备的密钥
char ssid[] = "asus_new";//这里填写你的wifi名字
char pswd[] = "1234qwer";//这里填写你的WiFi密码

//暂存温度、风速、模式数据
int nowTemperature = 25; //当前温度
int nowMode = 2; //默认制冷
int nowWindSpeed = 3;  //3档风速
bool nowPower = false;
bool nowWindTD = false;
bool nowLight = false;

// 新建组件对象
BlinkerNumber windSpeed("wind-speed-now"); //风速
BlinkerNumber temperature("temperature-now"); //温度
BlinkerButton setPower("btn-pwr");//开关
BlinkerButton setMode("btn-mode");//模式
BlinkerButton setLight("btn-light");//灯光
BlinkerButton setWindTD("btn-windtd");//上下风
BlinkerSlider sliderTemperature("ran-temperature");//温度滑块
BlinkerSlider sliderWindSpeed("ran-wind-speed");//风速滑块

//ESP8266的D2脚针
const uint16_t kIrLed = 4;
IRElectraAc ac(kIrLed);
// IRGreeAC ac(kIrLed);

void setup()
{
  Serial.begin(115200);
  //打开调试模式
  // BLINKER_DEBUG.stream(Serial);
  //输出所有调试信息
  // BLINKER_DEBUG.debugAll();
  ac.begin();
  // Blinker.begin();
  setPower.attach(SetPowerCallback);
  setMode.attach(SetModeCallback);
  setLight.attach(SetLightCallback);
  setWindTD.attach(SetWindTDCallback);
  sliderTemperature.attach(SliderTemperatureCallback);
  sliderWindSpeed.attach(SliderWindSpeedCallback);
  Serial.println("Default state of the remote.");
  Serial.println("Setting desired state for A/C.");
  Blinker.attachHeartbeat(heartbeat);
  Blinker.begin(auth,ssid,pswd); 

}

void loop()
{
  Blinker.run(); 
}

//空调电源开关
void SetPowerCallback(const String &state)
{
  Serial.println("power");
  if (state == BLINKER_CMD_ON)
  {
    ac.on();
    ac.setFan(nowWindSpeed);
    ac.setMode(kElectraAcAuto);
    if(nowMode==2){
      ac.setMode(kElectraAcCool);
    }
    if(nowMode==5){
      ac.setMode(kElectraAcHeat);
    }
    ac.setTemp(nowTemperature);
    ac.setLightToggle(nowLight);
    ac.send();
    setPower.icon("fal fa-power-off");
    setPower.color("#00FF00");
    setPower.text("当前状态:运行");
    setPower.print("on");
    nowPower = true;
  }
  else if (state == BLINKER_CMD_OFF)
  {
    ac.off();
    ac.setLightToggle(nowLight);
    // ac.setXFan(false);
    ac.send();
    setPower.icon("fal fa-power-off");
    setPower.color("#FF0000");
    setPower.text("当前状态:停止");
    setPower.print("off");
    nowPower = false;
  }
}

//空调灯光
void SetLightCallback(const String &state)
{
  Serial.println("light");
  if (state == BLINKER_CMD_ON)
  {
    setLight.color("#00FF00");
    setLight.text("灯光已打开");
    setLight.print("on");
    nowLight = true;
  }
  else if (state == BLINKER_CMD_OFF)
  {
    setLight.color("#999999");
    setLight.text("灯光已关闭");
    setLight.print("off");
    nowLight = false;
  }
  ac.setLightToggle(nowLight);
  ac.send();
}

//上下扇风
void SetWindTDCallback(const String &state)
{
  Serial.println("windtd");
  if (state == BLINKER_CMD_ON)
  {
    setWindTD.color("#00FF00");
    setWindTD.text("上下风已开启");
    setWindTD.print("on");
    nowWindTD = true;
  }
  else if (state == BLINKER_CMD_OFF)
  {
    setWindTD.color("#999999");
    setWindTD.text("上下风已关闭");
    setWindTD.print("off");
    nowWindTD = false;
  }
  ac.setSwingV(nowWindTD);
  ac.send();
}

//心跳包
void heartbeat()
{
  switch (nowMode)
  {
    case 1:
      setMode.icon("far fa-recycle");
      setMode.color("#7FFFD4");
      setMode.text("自动");
      break;
    case 2:
      setMode.icon("fas fa-snowflake");
      setMode.color("#1E90FF");
      setMode.text("当前制冷");
      break;
    case 3:
      setMode.icon("fas fa-tint");
      setMode.color("#7CFC00");
      setMode.text("除湿");
      break;
    case 4:
      setMode.icon("fab fa-first-order-alt");
      setMode.color("#E0FFFF");
      setMode.text("送风");
      break;
    case 5:
      setMode.icon("fas fa-sun");
      setMode.color("#FF0000");
      setMode.text("当前制热");
      break;
  }
  setMode.print();
  temperature.print(nowTemperature);
  windSpeed.print(nowWindSpeed);
  sliderTemperature.print(nowTemperature);
  sliderWindSpeed.print(nowWindSpeed);
  if(nowPower){
    setPower.icon("fal fa-power-off");
    setPower.color("#00FF00");
    setPower.text("当前状态:运行");
    setPower.print("on");
  }else{
    setPower.icon("fal fa-power-off");
    setPower.color("#FF0000");
    setPower.text("当前状态:停止");
    setPower.print("off");
  }
  if(nowWindTD){
    setWindTD.color("#00FF00");
    setWindTD.text("上下风已开启");
    setWindTD.print("on");
  }else{
    setWindTD.color("#999999");
    setWindTD.text("上下风已关闭");
    setWindTD.print("off");
  }
  if(nowLight){
    setWindTD.color("#00FF00");
    setWindTD.text("上下风已开启");
    setWindTD.print("on");
  }else{
    setLight.color("#999999");
    setLight.text("灯光已关闭");
    setLight.print("off");
  }
}
//空调模式按钮
void SetModeCallback(const String &state)
{
  Serial.println("setmode");
  if (state == BLINKER_CMD_BUTTON_TAP)
  {
    // nowMode++;
    // if (nowMode >= 6)
    // {
    //   nowMode = 2;
    // }
    if(nowMode==2){
      nowMode = 5;
    }else{
      nowMode = 2;
    }
    switch (nowMode)
    {
      case 1:
        ac.setMode(kElectraAcAuto);
        setMode.icon("fal fa-recycle");
        setMode.color("#008000");
        setMode.text("自动");
        break;
      case 2:
        ac.setMode(kElectraAcCool);
        setMode.icon("fal fa-snowflake");
        setMode.color("#1E90FF");
        setMode.text("当前制冷");
        break;
      case 3:
        ac.setMode(kElectraAcDry);
        setMode.icon("fal fa-tint-slash");
        setMode.color("#87CEFA");
        setMode.text("除湿");
        break;
      case 4:
        ac.setMode(kElectraAcFan);
        setMode.icon("fab fa-first-order-alt");
        setMode.color("#2E8B57");
        setMode.text("送风");
        break;
      case 5:
        ac.setMode(kElectraAcHeat);
        setMode.icon("fas fa-sun");
        setMode.color("#FF0000");
        setMode.text("当前制热");
        break;
    }
    setMode.print();
    if(nowPower){
      ac.send();
    }
  }
}


//空调风速
void SliderWindSpeedCallback(int32_t value)
{
  Serial.println("windspeed");
  nowWindSpeed = value;
  if(value==5){
    windSpeed.unit("自动");
  }else{
    windSpeed.unit("档");
  }
  windSpeed.print(nowWindSpeed);
  ac.setFan(nowWindSpeed);
  // ac.setXFan(true);
  ac.send();
}

//空调温度
void SliderTemperatureCallback(int32_t value)
{
  Serial.println("temp");
  nowTemperature=value;
  temperature.print(nowTemperature);
  ac.setTemp(nowTemperature);
  ac.send();
}